use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Create sys_log_login table
        manager
            .create_table(
                Table::create()
                    .table(SysLogLogin::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysLogLogin::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysLogLogin::Username).string().string_len(100))
                    .col(ColumnDef::new(SysLogLogin::Ip).string().string_len(100))
                    .col(ColumnDef::new(SysLogLogin::Address).string().string_len(100))
                    .col(ColumnDef::new(SysLogLogin::UserAgent).text())
                    .col(ColumnDef::new(SysLogLogin::Status).integer())
                    .col(ColumnDef::new(SysLogLogin::Operation).integer())
                    .col(ColumnDef::new(SysLogLogin::TenantId).big_integer())
                    .col(ColumnDef::new(SysLogLogin::CreatedAt).timestamp_with_time_zone().not_null())
                    .to_owned(),
            )
            .await?;

        // Create sys_log_operate table
        manager
            .create_table(
                Table::create()
                    .table(SysLogOperate::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysLogOperate::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysLogOperate::UserId).big_integer())
                    .col(ColumnDef::new(SysLogOperate::RealName).string())
                    .col(ColumnDef::new(SysLogOperate::Module).string())
                    .col(ColumnDef::new(SysLogOperate::Name).string())
                    .col(ColumnDef::new(SysLogOperate::ReqUri).string())
                    .col(ColumnDef::new(SysLogOperate::ReqMethod).string())
                    .col(ColumnDef::new(SysLogOperate::ReqParams).text())
                    .col(ColumnDef::new(SysLogOperate::Ip).string())
                    .col(ColumnDef::new(SysLogOperate::Address).string())
                    .col(ColumnDef::new(SysLogOperate::UserAgent).text())
                    .col(ColumnDef::new(SysLogOperate::TenantId).big_integer())
                    .col(ColumnDef::new(SysLogOperate::CreatedAt).timestamp_with_time_zone().not_null())
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysLogOperate::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysLogLogin::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum SysLogLogin {
    Table,
    Id,
    Username,
    Ip,
    Address,
    UserAgent,
    Status,
    Operation,
    TenantId,
    CreatedAt,
}

#[derive(DeriveIden)]
enum SysLogOperate {
    Table,
    Id,
    UserId,
    RealName,
    Module,
    Name,
    ReqUri,
    ReqMethod,
    ReqParams,
    Ip,
    Address,
    UserAgent,
    TenantId,
    CreatedAt,
}